/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.gate.repository;

import org.cshoo.tattoo.gate.entity.Authority;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
public interface IAuthorityRepository extends CrudRepository<Authority, Long> {

    /**
     * 根据访问范围查询权限列表
     * @param accessScope 访问范围
     * @return 权限列表
     */
    List<Authority> findAuthoritiesByAccessScope(Integer accessScope);

    /**
     * 根据用户名查询后台权限点
     *
     * @param userId 用户id
     * @return 权限点
     */
    @Query("select authority from Authority authority inner join RoleAuthorityRelation rar on authority.id = rar.authorityId inner join GroupRoleRelation grr on rar.roleId = grr.roleId inner join GroupUserRelation gur on grr.groupId = gur.groupId where gur.userId = ?1")
    List<Authority> findAuthoritiesByUserId(Long userId);

}
